package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import com.tanhua.service.RecommendUserApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@DubboService
public class RecommendUserApiImpl  implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //查询缘分值最高的小姐姐
    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
/*        //根据toUserId查询，根据评分score排序，获取第一条

        //构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        //调用mongoTemplate查询

        return mongoTemplate.findOne(query,RecommendUser.class);*/
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(1);
        return mongoTemplate.findOne(query, RecommendUser.class);

    }

    //分页查找推荐好友
    @Override
    public PageResult queryRecommendUserList(Long toUserId, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        Query query = Query.query(criteria);
        long count = mongoTemplate.count(query, RecommendUser.class);
        query.skip((page - 1) * pagesize).limit(pagesize);
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        PageResult pageResult = new PageResult(page, pagesize, count, list);
        return pageResult;
    }

    //查询用户与当前用户的缘分值
    @Override
    public RecommendUser getUserScore(Long userId, Long toUserId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("toUserId").is(toUserId);
        Query query = Query.query(criteria);
        RecommendUser r = mongoTemplate.findOne(query, RecommendUser.class);

        return r;
    }

    @Override
    public List<RecommendUser> getRecommendFriend(Long id) {
        //先从user_like表中查询出喜欢过和不喜欢的用户
        Query userId = Query.query(Criteria.where("userId").is(id));
        List<UserLike> userLikes = mongoTemplate.find(userId, UserLike.class);

        List<Long> collect = userLikes.stream().map(u -> u.getLikeUserId()).collect(Collectors.toList());
        //取出集合中的likeuserid作为排除条件
        Query query = Query.query(Criteria.where("toUserId").is(id).and("userId").nin(collect));
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);

        return list;
    }

    //找出用户与访问我的用户的缘分值
    @Override
    public Map<Long, RecommendUser> getUsers(List<Long> visitorUserId, Long userId) {
        //设置条件
        Criteria criteria = Criteria.where("userId").in(visitorUserId).and("toUserId").is(userId);
        List<RecommendUser> list = mongoTemplate.find(Query.query(criteria), RecommendUser.class);
        Map<Long, RecommendUser> collect = list.stream().collect(Collectors.toMap(r -> r.getUserId(), Function.identity()));
        return collect;
    }
}